package com.system.server;

import java.sql.ResultSet;
import java.sql.SQLException;

public class FoodDAO {
    //插入一个食品商品的基本信息
    public static void insertFood(Food food){
        String sql="insert into `food`(food_name,food_kind,food_value,food_number,food_sale,food_info,food_url) values('"+food.getFood_name()+"','"+food.getFood_kind()+"','"+food.getFood_value()+"','"+food.getFood_number()+"','"+food.getFood_sale()+"','"+food.getFood_info()+"','"+food.getFood_url()+"')";
        DataBaseControl.execute(sql);
        return;
    }

    public static ResultSet selectAllFood(){
        String sql="select * from `food`";
        return DataBaseControl.execute(sql);
    }

    public static void deleteFood(String foodName){
        String sql="delete from `food` where food_name='"+foodName+"'";
        DataBaseControl.execute(sql);
        return;
    }
    //通过食品名获取全部信息
    public static ResultSet selectWithFoodName(String name){
        String sql="select * from `food` where food_name='"+name+"'";
        return DataBaseControl.execute(sql);
    }
    //通过id获取全部信息
    public static ResultSet selectWithFoodID(String id){
        int insertID=Integer.parseInt(id);
        String sql="select * from `food` where food_id="+insertID+"";
        return DataBaseControl.execute(sql);
    }
    //通过食品名修改食品价格
    public static void updateValue(String name,String newValue){
        String sql="update `food` set food_value='"+newValue+"'where food_name='"+name+"'";
        DataBaseControl.execute(sql);
        return;
    }
    //修改库存
    public static void updateNumber(String name,String number){
        ResultSet res=selectWithFoodName(name);
        try{
            while(res.next()){
                String nowNumberStr=res.getString("food_number");
                int newNumber=Integer.parseInt(nowNumberStr)-Integer.parseInt(number);
                String newNumberStr=Integer.toString(newNumber);
                String sql="update `food` set food_number='"+newNumberStr+"'where food_name='"+name+"'";

                DataBaseControl.execute(sql);
                return;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return;
    }
    //修改销量
    public static void updateSale(String name,String sale){
        ResultSet res=selectWithFoodName(name);
        try{
            while(res.next()){
                String nowSaleStr=res.getString("food_sale");
                int newSale=Integer.parseInt(nowSaleStr)+Integer.parseInt(sale);
                String newSaleStr=Integer.toString(newSale);
                String sql="update `food` set food_sale='"+newSaleStr+"'where food_name='"+name+"'";

                DataBaseControl.execute(sql);
                return;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return;
    }
    //修改种类
    public static void updateKind(String name,String kind){
        String sql="update `food` set food_kind='"+kind+"'where food_name='"+name+"'";
        DataBaseControl.execute(sql);
        return;
    }
    //修改商品简介
    public static void updateInfo(String name,String info){
        String sql="update `food` set food_info='"+info+"'where food_name='"+name+"'";
        DataBaseControl.execute(sql);
        return;
    }
    //得到有多少行数据，即有多少个商品
    public static int getFoodCount(){
        String sql="select * from `food`";
        ResultSet res=DataBaseControl.execute(sql);
        int rowCount=0;
        try{
            while(res.next()){
                rowCount++;
            }
            return rowCount;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }
}
